package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.commons.exception.BizException;
import com.cskaoyan.mall.dto.ItemDto;
import com.cskaoyan.mall.dto.ProductDetailDto;
import com.cskaoyan.mall.dto.ProductDto;
import com.cskaoyan.shopping.constants.ShoppingRetCode;
import com.cskaoyan.shopping.converter.ContentConverter;
import com.cskaoyan.shopping.converter.ProductConverter;
import com.cskaoyan.shopping.dal.entitys.*;
import com.cskaoyan.shopping.dal.persistence.ItemDescMapper;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dal.persistence.PanelMapper;
import com.cskaoyan.shopping.dto.AllProductResponse;
import com.cskaoyan.shopping.dto.PanelContentItemDto;
import com.cskaoyan.shopping.dto.PanelDto;
import com.cskaoyan.shopping.form.PageRequest;
import com.cskaoyan.shopping.service.IProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class IProductServiceImpl implements IProductService {


    @Autowired
    ItemMapper itemMapper;

    @Autowired
    ProductConverter productConverter;

    @Autowired
    ItemDescMapper itemDescMapper;

    @Autowired
    PanelMapper panelMapper;

    @Autowired
    PanelContentMapper panelContentMapper;

    @Autowired
    ContentConverter contentConverter;


    @Override
    public ProductDetailDto getProductDetail(Long productId) {
        Item item = itemMapper.selectByPrimaryKey(productId);
        if (item == null) {
            // 未找到id对应商品
            throw new BizException(ShoppingRetCode.PRODUCT_NOT_EXISTS.getCode(),
                    ShoppingRetCode.PRODUCT_NOT_EXISTS.getMessage());
        }

        ItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(productId);


        ProductDetailDto productDetailDto = productConverter.toProductDetailDto(item,itemDesc);
        return productDetailDto;

    }

    @Override
    public AllProductResponse getAllProduct(PageRequest pageInfo) {

       /* Condition condition = new Condition(Item.class);

        String sort = pageInfo.getSort();

        if (sort == null) {

            condition.setOrderByClause("created desc");
        } else if (Integer.valueOf(sort) == 1) {
            condition.setOrderByClause("price asc");
        } else if (Integer.valueOf(sort) == -1) {
            condition.setOrderByClause("price desc");
        }*/


        Example example = new Example(Item.class);

        String sort = pageInfo.getSort();

        //  sort="1"升序或者"-1"降序,未点击"从低到高排序"或从“从高到低排序”时，前端传递的值为null，根据created字段排序，排序方式为降序
        if ("".equals(sort)) {

            example.setOrderByClause("created desc");
        } else if (Integer.valueOf(sort) == 1) {
            example.setOrderByClause("price asc");
        } else if (Integer.valueOf(sort) == -1) {
            example.setOrderByClause("price desc");
        }

        Example.Criteria criteria = example.createCriteria();

        /*if (pageInfo.getPriceLte() != null && pageInfo.getPriceGt() != null) {

            criteria.andBetween("price",  pageInfo.getPriceGt(),pageInfo.getPriceLte());
        }*/
        if (pageInfo.getPriceGt() != null) {
            criteria.andGreaterThanOrEqualTo("price", pageInfo.getPriceGt());
        }
        if (pageInfo.getPriceLte() != null) {
            criteria.andLessThanOrEqualTo("price", pageInfo.getPriceLte());
        }
        if (pageInfo.getPriceLte() == null && pageInfo.getPriceGt() != null) {

            // criteria.andBetween("price",  pageInfo.getPriceGt(),);
        }
        if (pageInfo.getPriceLte() != null && pageInfo.getPriceGt() != null) {

            criteria.andBetween("price",  pageInfo.getPriceGt(),pageInfo.getPriceLte());
        }


        if (pageInfo.getCid() != null) {
            criteria.andEqualTo("cid", pageInfo.getCid());
        }
        //example.createCriteria().andBetween("price", pageInfo.getPriceLte(), pageInfo.getPriceGt()).andEqualTo("cid",pageInfo.getCid());

//        RowBounds rowBounds = new RowBounds((pageInfo.getPage() - 1) * pageInfo.getSize(), pageInfo.getSize());
//        List<Item> items = itemMapper.selectByExampleAndRowBounds(example, rowBounds);


        PageHelper.startPage((pageInfo.getPage() - 1) * pageInfo.getSize(), pageInfo.getSize());
        List<Item> items = itemMapper.selectByExample(example);
        Long total = new PageInfo<>(items).getTotal();

        List<ProductDto> productDtos = new ArrayList<>();



        //   查询到的结果toDto
        for (Item item : items) {
            ProductDto productDto = new ProductDto();

            productDto.setProductId(item.getId());
            productDto.setSalePrice(item.getPrice());
            productDto.setProductName(item.getTitle());
            productDto.setSubTitle(item.getSell_point());
            String image = item.getImage();
            String[] images = image.split(",");
            productDto.setPicUrl(images[0]);
            productDtos.add(productDto);
        }
        // 计算条目放入total
       // Long total = (long) productDtos.size();


        AllProductResponse response = new AllProductResponse();
        response.setData(productDtos);
        response.setTotal(total);
        return  response;


    }

    @Override
    public List<PanelDto> getRecommendGoods() {

        List<Panel> panels = panelMapper.selectPanelContentByPosition(1);

        List<PanelDto> panelDtos = new ArrayList<>();


        for (Panel panel : panels) {
            PanelDto panelDto = contentConverter.panel2Dto(panel);

            List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(panel.getId());
            List<PanelContentItemDto> panelContentItemDtos = contentConverter.panelContentItem2Dto(panelContentItems);
            panelDto.setPanelContentItems(panelContentItemDtos);
            panelDtos.add(panelDto);
        }



        return panelDtos;
    }

    @Override
    public List<ItemDto> getAllItems() {
        return null;
    }
}
